/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Modelo.ConexaoBD;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author pretto
 */
public class Combos {

    Connection con = null;
    Statement stmt = null;
    ResultSet result = null;
    int codigo = 0;
    String descricao = null;
    HashMap<String, Itens> map = new HashMap<String, Itens>();

    public Combos(String tabela) {
        abreConexao();
        try {
            result = stmt.executeQuery("select * from " + tabela + "");
            while (result.next()) {
                codigo = result.getInt(1);
                descricao = result.getString(2);
                Itens i = new Itens(codigo, descricao);
                map.put(descricao, i);
            }
        } catch (Exception e) {
            System.out.print(e);
        }
    }

    public Combos(String tabela, String coluna, int valor) {
        
        abreConexao();
        try {
            result = stmt.executeQuery("select * from " + tabela + " WHERE "+coluna+" = '"+valor+"'");
            while (result.next()) {
                codigo = result.getInt(1);
                descricao = result.getString(2);
                Itens i = new Itens(codigo, descricao);
                map.put(descricao, i);
            }
        } catch (Exception e) {
            System.out.print(e);
        }
    }

    private void abreConexao() {
        try {
            con = ConexaoBD.getInstance().getConnection();
            stmt = con.createStatement();
        } catch (Exception e) {
            System.out.print(e);
        }
    }

    public Map<String, Itens> getMap() {
        return map;
    }

    public class Itens {

        int codigo;
        String descricao;

        public Itens(int c, String d) {
            codigo = c;
            descricao = d;
        }

        public int getCodigo() {
            return codigo;
        }

        public String getDescricao() {
            return descricao;
        }
    }
}
